package com.seasonsfood.mall.web.dispatch.controller.dispatch;

import com.alibaba.dubbo.config.annotation.Reference;
import com.seasonsfood.mall.service.dispatch.api.domain.DispSite;
import com.seasonsfood.mall.service.dispatch.api.model.DispSiteResp;
import com.seasonsfood.mall.service.dispatch.api.param.DispSiteRequest;
import com.seasonsfood.mall.service.dispatch.api.param.StreetAndSite;
import com.seasonsfood.mall.service.dispatch.api.service.DispCircuitService;
import com.seasonsfood.mall.service.dispatch.api.service.DispSiteService;
import com.seasonsfood.mall.util.constant.ResponseCode;
import com.seasonsfood.mall.util.exception.BizException;
import com.seasonsfood.mall.util.model.JsonResponse;
import com.seasonsfood.mall.util.model.ListResponse;
import com.seasonsfood.mall.util.util.ResponseUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("dispSite")
public class DispSiteController {

    @Reference(check = false)
    private DispSiteService dispSiteService;
    @Reference(check = false)
    private DispCircuitService dispCircuitService;

    //列表模式街道下小区
    @RequestMapping("siteList")
    public JsonResponse listModelSite(DispSiteRequest dispSiteRequest) {

        ListResponse<DispSiteResp> dispSiteRespListResponse = dispSiteService.listModelSiteList(dispSiteRequest);
        //如果在当前街道下找不到该小区就全表查询
        if (dispSiteRespListResponse.getRows().size() == 0) {
            //如果全表查也找不到就返回暂无数据
            ListResponse<StreetAndSite> streetAndSiteListResponse = dispSiteService.findSiteLocation(dispSiteRequest.getSiteName());
            if (streetAndSiteListResponse.getRows().size() == 0) {
                return ResponseUtils.setToJson(ResponseCode.NOT_DATA);
            }
            //有的话提示他在那个街道,有可能存在多种差不多名字的位于多个街道
            return ResponseUtils.setSuccess(streetAndSiteListResponse);
        }
        return ResponseUtils.setSuccess(dispSiteRespListResponse);

    }

    //搜索条件接口
    @RequestMapping("chooseCircuit")
    public JsonResponse chooseCircuit(String circuitName, Long dispatchId) {

        return ResponseUtils.setSuccess(dispCircuitService.chooseCircuit(circuitName, dispatchId));
    }

    //查找配送员(未完成)
    @RequestMapping("chooseDisp")
    public JsonResponse chooseDisptach(String dispatchName) {

        return ResponseUtils.setSuccess();
    }


    @RequestMapping("addSite")
    public JsonResponse addDispSite(DispSite dispSite, List<Long> circuitIdList) {

        try {
            ResponseCode responseCode = dispSiteService.addDispSite(dispSite, circuitIdList);
            if (!ResponseCode.SUCCESS.equals(responseCode)) {
                throw new BizException(responseCode);
            }
            return ResponseUtils.setSuccess();
        } catch (BizException e) {
            return ResponseUtils.setToJson(e.getResponseCode());
        } catch (IllegalArgumentException e) {
            return ResponseUtils.setJsonAndMsg(ResponseCode.NOT_NULL, e.getMessage(), true);
        }
    }

    //修改包括启用禁用,更改名字,更改街道,更改线路
    @RequestMapping("update")
    public JsonResponse update(DispSite dispSite, List<Long> circuitIdList) {
        try {
            ResponseCode responseCode = dispSiteService.updateDispSite(dispSite, circuitIdList);
            if (!ResponseCode.SUCCESS.equals(responseCode)) {
                throw new BizException(responseCode);
            }
            return ResponseUtils.setSuccess();
        } catch (BizException e) {
            return ResponseUtils.setToJson(e.getResponseCode());
        } catch (IllegalArgumentException e) {
            return ResponseUtils.setJsonAndMsg(ResponseCode.NOT_NULL, e.getMessage(), true);
        }
    }


}
